<template>
  <div class="content">
    <el-form ref="form" :model="form" label-width="80px" :inline="true">
      <el-form-item>
        <el-input clearable v-model="form.name" size="mini" placeholder="请输入姓名"></el-input>
      </el-form-item>

      <el-form-item>
        <el-input clearable v-model="form.jobNo" size="mini" placeholder="请输入工号"></el-input>
      </el-form-item>

      <el-form-item>
        <el-input clearable v-model="form.phone" size="mini" placeholder="请输入手机号码"></el-input>
      </el-form-item>

      <el-form-item>
        <el-input clearable v-model="form.idNo" size="mini" placeholder="请输入身份证号"></el-input>
      </el-form-item>

      <el-button type="primary" size="mini" class="btn" @click="search"><i class="fa fa-search" style="margin-right:5px;"></i>查询</el-button>
      <el-button type="primary" size="mini" @click="editAddOrUpdate(1)"><i class="fa fa-plus" style="margin-right:5px;"></i>新增</el-button>
      <el-button type="primary" size="mini" @click="downLoadDemo"><i class="fa fa-arrow-down" style="margin-right:5px;"></i>下载</el-button>

      <el-upload
        class="upload"
        :action="url+'/admin/teacher/upload'"
        :on-success="uploadSuccess"
        :headers="requestHeader"
        :limit="100"
        accept="xlsx"
        :show-file-list="false"
        :on-exceed="handleExceed"
      >
        <el-button size="mini" type="primary"><i class="fa fa-arrow-up" style="margin-right:5px;"></i>导入</el-button>
      </el-upload>
      <el-button type="primary" size="mini" @click="excelExport"><i class="fa fa-arrow-down" style="margin-right:5px;"></i>导出</el-button>
      <!-- <el-button type="primary" size="mini">创建教职工照片</el-button>
      <el-button type="primary" size="mini">导出教职工信息</el-button>-->
    </el-form>

    <!-- <el-divider></el-divider> -->

    <template>
      <el-table :data="tableData" v-loading="loading" height="470" size="mini" style="width: 100%">
        <el-table-column prop="id" fixed label="教职工ID" width="150"></el-table-column>
        <el-table-column prop="name" fixed label="姓名"></el-table-column>
        <el-table-column prop="balance" label="余额"></el-table-column>
        <el-table-column prop="emergencyPhone" width="150" label="手机号码"></el-table-column>
        <el-table-column prop="idNo" width="150" label="身份证号"></el-table-column>
        
        <!-- <el-table-column prop="gender" label="性别"></el-table-column>

        <el-table-column prop="nation" label="民族"></el-table-column>

        <el-table-column prop="birthday" width="100" label="出生年月">
          <template slot-scope="scope">
            <span>{{scope.row.birthday.slice(0,10)}}</span>
          </template>
        </el-table-column>

        <el-table-column prop="marriedFlg" label="婚否">
          <template slot-scope="scope">
            <span>{{scope.row.marriedFlg===true?"已婚":'未婚'}}</span>
          </template>
        </el-table-column> -->

        <!--        <el-table-column-->
        <!--          prop="graduateFrom"-->
        <!--          label="学校">-->
        <!--        </el-table-column>-->

        <!-- <el-table-column prop="graduateFrom" width="100" label="毕业院校"></el-table-column>

        <el-table-column prop="specialty" label="专业"></el-table-column>

        <el-table-column prop="degree" label="学历"></el-table-column>

        <el-table-column prop="graduateTime" width="100" label="毕业时间">
          <template slot-scope="scope">
            <span>{{scope.row.graduateTime.slice(0,10)}}</span>
          </template>
        </el-table-column>

        <el-table-column prop="hiredate" width="100" label="入职时间">
          <template slot-scope="scope">
            <span>{{scope.row.hiredate.slice(0,10)}}</span>
          </template>
        </el-table-column>

        <el-table-column prop="employedYears" label="工龄"></el-table-column>

        <el-table-column prop="level" label="职称"></el-table-column> -->

        <!--        <el-table-column-->
        <!--          prop="marriedFlg"-->
        <!--          label="是否在编">-->
        <!--        </el-table-column>-->

        <!-- <el-table-column prop="status" label="状态"></el-table-column>

        <el-table-column prop="duty" label="职务"></el-table-column>

        <el-table-column prop="course" label="任教学科"></el-table-column> -->

        <!-- <el-table-column prop="emergencyContact" label="紧急联系人"></el-table-column>

        <el-table-column prop="nativePlace" label="籍贯"></el-table-column>

        <el-table-column prop="address" label="家庭住址"></el-table-column> -->

        <el-table-column fixed="right" :label="$t('action.operation')" width="210">
          <template slot-scope="scope">
            <el-button size="mini" @click="editAddOrUpdate(2,scope.row.id)"><i class="fa fa-edit" style="margin-right:5px;"></i>{{$t('action.edit')}}</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            ><i class="fa fa-trash" style="margin-right:5px;"></i>{{$t('action.delete')}}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-row>
      <el-col :span="24">
        <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :page-sizes="[10, 15, 20,50,100]"
              :page-size="everyPageNum"
              layout="total, sizes,prev, pager, next, jumper"
              :total="total"
            ></el-pagination>
        <div class="grid-content bg-purple-dark" style="font-size:13px;float:right;margin:26px 0 0 10px">合计金额：<span style="color:rgb(20, 136, 154);font-weight:bold">{{sumData}}</span></div>
      </el-col>
    </el-row>
    <!--新增编辑弹框-->
    <el-dialog :title="updateStatus==1?'创建教职工信息':'编辑教职工信息'" :visible.sync="showEdit" width="60%" v-dialogDrag>
      <editTeacher @hideEdit="hideEditStatus" ref="child" :msg="updateStatus"></editTeacher>
    </el-dialog>
  </div>
</template>

<script>
import editTeacher from "@/components/TeacherManagement/edit";
import Cookies from "js-cookie";

export default {
  name: "TeacherManagement",
  components: {
    editTeacher
  },
  data() {
    return {
      url: this.$Config.url,
      loading: false,
      total: 1, //总页数
      updateStatus: 1, //默认是新增
      everyPageNum: 10, //每页多少条
      currentPage: 1, //当前页
      showEdit: false, //显示编辑弹框
      form: {
        name: "",
        jobNo: "",
        phone: "",
        idNo: ""
      },
      tableData: [],
      requestHeader: {
        "x-baie-admin-token": Cookies.get("token")
      },
      sumData: ''
    };
  },
  mounted() {
    this.common();
  },
  methods: {
    excelExport() {
      const deptId = JSON.parse(localStorage.getItem("userInfo")).deptId;
      // if (JSON.parse(localStorage.getItem("userInfo")).deptId) {
        var divFrame = window.parent.document.getElementById(
          "downLoadListFrame"
        );
        //判断是否存在，如果存在先移除，再重新创建
        if (divFrame != null) {
          window.parent.document.body.removeChild(divFrame);
        }
        var iframe = window.parent.document.createElement("iframe");
        iframe.setAttribute("id", "downLoadListFrame");
        //download_file.id = "downFrame";
        window.parent.document.body.appendChild(iframe);
        divFrame = window.parent.document.getElementById("downLoadListFrame");
        //隐式调用，（注意：window.parent.document 适应对于，farme内部代码，调用frame 外部dom；如果项目未用frame，改写成 document即可）
        divFrame.src = `${this.url}/admin/teacher/export?name=${this.form.name}&phone=${this.form.phone}&idNo=${this.form.idNo}&jobNo=${this.form.jobNo}`;
        divFrame.style.display = "none";
      // } else {
      //   this.$message({
      //     message: "超级管理员无法下载模板",
      //     type: "warning"
      //   });
      // }
    },
    downLoadDemo() {
      const deptId = JSON.parse(localStorage.getItem("userInfo")).deptId;
      if (JSON.parse(localStorage.getItem("userInfo")).deptId) {
        var divFrame = window.parent.document.getElementById(
          "downLoadListFrame"
        );
        //判断是否存在，如果存在先移除，再重新创建
        if (divFrame != null) {
          window.parent.document.body.removeChild(divFrame);
        }
        var iframe = window.parent.document.createElement("iframe");
        iframe.setAttribute("id", "downLoadListFrame");
        //download_file.id = "downFrame";
        window.parent.document.body.appendChild(iframe);
        divFrame = window.parent.document.getElementById("downLoadListFrame");
        //隐式调用，（注意：window.parent.document 适应对于，farme内部代码，调用frame 外部dom；如果项目未用frame，改写成 document即可）
        divFrame.src = this.url + "/admin/excel/teacher?deptId=" + deptId;
        divFrame.style.display = "none";
      } else {
        this.$message({
          message: "超级管理员无法下载模板",
          type: "warning"
        });
      }
    },
    uploadSuccess(response, file, fileList) {
      if (response.code == 200) {
        this.$message({
          message: "导入成功",
          type: "success"
        });
        this.common(this.currentPage, this.everyPageNum);
      } else {
        this.$message({
          message: response.msg,
          type: "error"
        });
      }
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      );
    },
    handleDelete(index, row) {
      this.$confirm("您确认要删除该教师?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteTeacher(row.id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    deleteTeacher(id) {
      const that = this;
      this.$axios
        .post(
          that.url + "/admin/teacher/delete/{id}",
          that.$qs.stringify({ id }),
          {
            headers: {
              "x-baie-admin-token": Cookies.get("token")
            }
          }
        )
        .then(function(res) {
          if (res.data.code == 200) {
            that.$message({
              type: "success",
              message: "删除成功!"
            });
            that.common(that.currentPage, that.everyPageNum);
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    search() {
      this.common(this.currentPage, this.everyPageNum);
    },
    common(pageNo = 1, pageSize = 10) {
      const that = this;
      this.loading = true;
      this.$axios
        .get(that.url + "/admin/teacher/page", {
          params: {
            pageNo,
            pageSize,
            name: that.form.name,
            jobNo: that.form.jobNo,
            phone: that.form.phone,
            idNo: that.form.idNo
          },
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        })
        .then(function(res) {
          that.loading = false;
          if (res.data.code == 200) {
            that.tableData = res.data.data.records;
            that.total = res.data.data.total;
            that.sumData = res.data.sum;
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(function(error) {
          that.loading = false;
        });
    },
    handleClick(row) {
      console.log(row);
    },
    handleSizeChange(val) {
      //   每页多少条
      this.everyPageNum = val;
      this.common(this.currentPage, val);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.common(val, this.everyPageNum);
    },
    editAddOrUpdate(status, id) {
      //新增或编辑
      /**
       * 1是新增
       * 2是编辑
       */
      this.showEdit = true;
      if (status == 1) {
        //新增
        this.updateStatus = 1;
        setTimeout(() => {
          this.$refs["child"].resetForm("ruleForm");
        }, 0);
      } else {
        //编辑
        this.updateStatus = 2;
        setTimeout(() => {
          this.$refs["child"].resetData(id);
        }, 0);
      }
    },
    hideEditStatus(status) {
      this.showEdit = false;
      this.common(this.currentPage, this.everyPageNum);
    }
  }
};
</script>

<style scoped lang="scss">
.upload {
  display: inline-block;
}
.el-pagination {
  float: right;
  margin: 20px 10px;
}
.el-input {
  width: 193px;
}
.button_list {
  margin-bottom: 10px;
}

.content {
  text-align: left;
}

.el-divider {
  margin: 10px 0;
}

.btn {
  margin-top: 6px;
}

.el-form-item {
  margin-bottom: 0;
}
</style>
